local xiejin = fk.CreateSkill{
  name = "hanqing__xiejin",
  tags = { Skill.Switch },
  attached_skill_name = "hanqing__xiejin&",
}

Fk:loadTranslationTable{
  ["hanqing__xiejin"] = "胁进",
  [":hanqing__xiejin"] = "转换技，①你可以弃置场上一张黑色牌，视为使用【无懈可击】；②所有角色可以经你允许视为使用【过河拆桥】，若目标不为你，"..
  "则须先弃置一张牌。",

  [":hanqing__xiejin_yang"] = "转换技，<font color=\"#E0DB2F\">①你可以弃置场上一张黑色牌，视为使用【无懈可击】；</font>"..
  "<font color=\"gray\">②所有角色可以经你允许视为使用【过河拆桥】，若目标不为你，则须先弃置一张牌。</font>",
  [":hanqing__xiejin_yin"] = "转换技，<font color=\"gray\">①你可以弃置场上一张黑色牌，视为使用【无懈可击】；</font>"..
  "<font color=\"#E0DB2F\">②所有角色可以经你允许视为使用【过河拆桥】，若目标不为你，则须先弃置一张牌。</font>",

  ["#hanqing__xiejin-yang"] = "胁进：弃置场上一张黑色牌，视为使用【无懈可击】",
  ["#hanqing__xiejin-yin"] = "胁进：视为对李兑使用【过河拆桥】，或弃置一张牌视为对李兑以外的角色使用【过河拆桥】（均需经李兑同意）",
  ["#hanqing__xiejin-ask"] = "胁进：是否允许 %dest 使用【过河拆桥】？",

  ["$hanqing__xiejin1"] = "赖君等之臂力，清彼章之虫豸，德莫大焉！",
  ["$hanqing__xiejin2"] = "公为头阵，勉之！事败为叛贼，事成为勋臣。",
}

xiejin:addEffect("viewas", {
  mute = true,
  pattern = "dismantlement,nullification",
  prompt = function (self, player)
    return "#hanqing__xiejin-"..player:getSwitchSkillState(xiejin.name, false, true)
  end,
  expand_pile = function (self, player)
    local cards = {}
    if player:getSwitchSkillState(xiejin.name, false) == fk.SwitchYang then
      for _, p in ipairs(Fk:currentRoom().alive_players) do
        local flag = p == player and "j" or "ej"
        table.insertTable(cards, table.filter(p:getCardIds(flag), function (id)
          return Fk:getCardById(id).color == Card.Black
        end))
      end
    end
    return cards
  end,
  card_filter = function(self, player, to_select, selected)
    if #selected == 0 then
      if player:getSwitchSkillState(xiejin.name, false) == fk.SwitchYang then
        return not table.contains(player:getCardIds("h"), to_select) and
          Fk:getCardById(to_select).color == Card.Black
      elseif player:getSwitchSkillState(xiejin.name, false) == fk.SwitchYin then
        return not player:prohibitDiscard(to_select)
      end
    end
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 then return end
    local name = player:getSwitchSkillState(xiejin.name, false) == fk.SwitchYang and "nullification" or "dismantlement"
    local card = Fk:cloneCard(name)
    card.skillName = xiejin.name
    if name == "dismantlement" then
      table.insert(card.skillNames, "hanqing__xiejin_other")
    end
    card:addFakeSubcards(cards)
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    room:notifySkillInvoked(player, xiejin.name, "switch")
    if use.card.name == "nullification" then
      player:broadcastSkillInvoke(xiejin.name, 1)
    else
      player:broadcastSkillInvoke(xiejin.name, 2)
    end
    local to
    for _, p in ipairs(room.alive_players) do
      if table.contains(p:getCardIds("hej"), use.card.fake_subcards[1]) then
        to = p
        break
      end
    end
    room:throwCard(use.card.fake_subcards, xiejin.name, to, player)
  end,
  enabled_at_play = function (self, player)
    return player:getSwitchSkillState(xiejin.name, false) == fk.SwitchYin
  end,
  enabled_at_response = function (self, player, response)
    if response then return end
    if player:getSwitchSkillState(xiejin.name, false) == fk.SwitchYang then
      return table.find(Fk:currentRoom().alive_players, function (p)
        return table.find(p:getCardIds("ej"), function (id)
          return Fk:getCardById(id).color == Card.Black
        end) and
        #player:getViewAsCardNames(xiejin.name, {"nullification"}) > 0
      end)
    elseif player:getSwitchSkillState(xiejin.name, false) == fk.SwitchYin then
      return #player:getViewAsCardNames(xiejin.name, {"dismantlement"}) > 0
    end
  end,
  enabled_at_nullification = function (self, player, data)
    return player:getSwitchSkillState(xiejin.name, false) == fk.SwitchYang and
      table.find(Fk:currentRoom().alive_players, function (p)
        return table.find(p:getCardIds("ej"), function (id)
          return Fk:getCardById(id).color == Card.Black
        end) ~= nil
      end) ~= nil
  end,
})

xiejin:addEffect("prohibit", {
  is_prohibited = function(self, from, to, card)
    if card and table.contains(card.skillNames, xiejin.name) and card.name == "dismantlement" then
      if table.contains(card.skillNames, "hanqing__xiejin_other") then
        return to:hasSkill(xiejin.name) and to:getSwitchSkillState(xiejin.name, false) == fk.SwitchYin
      else
        return not (to:hasSkill(xiejin.name) and to:getSwitchSkillState(xiejin.name, false) == fk.SwitchYin)
      end
    end
  end,
})

xiejin:addAI(nil, "vs_skill")

return xiejin
